<script type="text/javascript"><!--//--><![CDATA[//><!--

var LOVIspVendor5 = function(ao_option){
	try{
		if(ao_option==undefined){ ao_option={}; }
		var lf_onSelect	= (ao_option.onSelect==undefined || typeof(ao_option.onSelect)!='function') ? function(){} : ao_option.onSelect;
		
		// Create Popup
		var $popup	= openPopup({ title:'Daftar Kontrak' , width:700, height:370 });
		var ls_id	= $popup.attr('id');
		
		// Append Datagrid Container
		var ls_gridName	= 'grid_'+ls_id;
		$popup	.html(	$('<div>')	.attr('id', 'tb'+ls_id).attr('style','padding:5px;height:auto;')
									.html(	$('<div>').html($('<form>')	.attr('id','fm_search_'+ls_id).data('rel',ls_id).submit(function(e){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).data('rel')).serializeObject()); e.preventDefault(); })
																		.html('<table class="tb_form" style="margin-bottom:0px"><tr><td width="100">Keyword</td><td><input type="text" name="keyword" class="textfield medium" /><a href="#" class="easyui-linkbutton" iconCls="icon-search" id="btn_search'+ls_id+'" rel="'+ls_id+'">Search</a></td></tr></table>')
															)
											)
						)
				.append($('<div>').attr('id', ls_gridName).attr('toolbar', '#tb'+$popup.attr('id')));
		
		$('#btn_search'+ls_id).click(function(){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).attr('rel')).serializeObject()); return false; });
		$('#btn_search'+ls_id).linkbutton({  
			plain:true  
		}); 
		
		// Build Datagrid
		var ls_param = (ao_option.param==undefined ? '' : ao_option.param);
		$('#'+ls_gridName).datagrid({  
			url:gs_path+'/isp_vendor_invoice/getdata2/'+ls_param,
			queryParams: $('#fm_search_'+ls_id).serializeObject(),
			height:337,
			pagination:true,
			singleSelect:true,
			columns:[[
				{ field : 'kode_kontrak', title : 'Kode Kontrak', width : 100, sortable:true },
				{ field : 'kode_vendor', title : 'Kode Vendor', width : 100, sortable:true },
				{ field : 'nama_vendor', title : 'Nama Vendor', width : 300, sortable:true },
				{ field : 'harga', title : 'Harga', width : 100, sortable:true }
			]],
			onSelect:function(rowIndex, rowData){ lf_onSelect(rowData); closePopup(); }
		});
		
	} catch (err) { alert('Func. LOVSampleReference :\n' + err); }
};
	var grid_reload = function(){
		$('#datagrid').datagrid('reload', $('#fm_search').serializeObject());
	};

	$(document).ready(function() {
		// Setup Datagrid
		$('#datagrid').datagrid({  
			url:gs_path+'/'+controller+'/getdata', 
			queryParams: $('#fm_search').serializeObject(),
			height:340,
			pagination:true,
			columns:[[
				{ field : 'ck', checkbox:true, width: 40, align: 'center' },
				{ field : 'kode_invoice', title : 'Kode Invoice', width : 120, sortable:true },
				{ field : 'kode_kontrak', title : 'Kode Kontrak', width : 120, sortable:true },
				{ field : 'nama_vendor', title : 'Nama Vendor', width : 120, sortable:true },
				{ field : 'tgl_invoice', title : 'Tgl Invoice', width : 120, sortable:true },
				{ field : 'status', title : 'Status', width : 220, sortable:true },
				{ field : 'total_bayar', title : 'Total Bayar', width : 100, sortable:true },
				{ field : 'keterangan', title : 'Keterangan', width : 100, sortable:true }
			]]
		});		
		
		// Setup Form
		$('#form_master').setupForm([			
			{ name: 'kode_invoice', type: 'text', size: 'medium', maxlength: 15, noempty:true },
			{ name: 'kode_kontrak', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval('');
					//$('#kode_vendor').setval('');
					$('#nama_vendor').setval('');
					$('#harga').setval(''); 
				},
				onActionButton:function(){ 
					LOVIspVendor5({ onSelect:function(row){ 
						$('#kode_kontrak').setval(row.kode_kontrak); 
						$('#nama_vendor').setval(row.nama_vendor);
						$('#harga').setval(row.nama_vendor); 
					} }); 
				}
			},
			//{ name: 'kode_vendor', type: 'text', size: 'medium', maxlength: 15, noempty:true },
			{ name: 'nama_vendor', type: 'text', size: 'medium', maxlength: 15, noempty:true, readonly:true},
			{ name: 'harga', type: 'text', size: 'medium', maxlength: 15, noempty:true, readonly:true },
			{ name: 'tgl_invoice', type: 'date', noempty:true },
			{ name: 'status', type: 'text', size:'medium', maxlength:40 , noempty:true },
			{ name: 'total_bayar', type: 'text', size: 'medium', maxlength: 40, noempty:true },			
			{ name: 'keterangan', type: 'textarea', size:'long', rows:3 },
			{ name: 'urutan', value:'1', type: 'text', size: 'long', maxlength: 255 }
		]);
		
		// Create Dialog
		$('#dialog_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:600,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					$('#form_master').submitForm('/'+controller+'/ajax_post', { onComplete : function(){ $('#dialog_container').dialog('close'); $('#datagrid').datagrid('reload'); }} );
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#dialog_container').dialog('close');
				}
			}]
		});
		
		
		// Set Action Button
		var buttons = [ { label: 'Add', icon: 'add', onclick: function() {
							resetError();
							$('#dialog_container').dialog('open');
							$('#form_master').data('mode', 'create');
							$('#form_master').parent().parent().parent().prev().find('.panel-title').html('New Vendor Contract');
							$('#kode_invoice').readonly(false);
							$('#kode_invoice, #tgl_kontrak, #kode_kontrak, #nama_vendor, #harga, #tgl_awal, #tgl_akhir, #harga, #keterangan').val('');
							$('#kode_invoice').focus();
						} },
						{ label: 'Edit', icon: 'edit', onclick: function() {
							var oSel=$('#datagrid').datagrid('getSelected'); 
							if(oSel==null){ alert('Please select row!'); }
							else{ 
								resetError();
								$('#dialog_container').dialog('open'); 
								$('#form_master').data('mode', 'edit');
								$('#form_master').parent().parent().parent().prev().find('.panel-title').html('Edit Vendor Contract');
								$('#kode_invoice').readonly();
								jsonToForm(oSel); $('#tgl_kontrak').focus();
							} 
						} },
						{label: 'Delete', icon: 'delete', onclick: function() { 
							var oSel = $('#datagrid').datagrid('getSelected'); 
							if (oSel == null) { 
								$.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); 
							} else { 
								deleteData(['kode_invoice']);
							} 
						}}];
		setAction(buttons);
		
		
		$('#fm_search').focusFirst();
	});
//--><!]]></script>

<div id="tb" style="padding:5px;height:auto">
    <div>
        <form id="fm_search" onsubmit="grid_reload(); return false;">
        	<table class="tb_form" style="margin-bottom:0px">
            <tr>
            	<td width="100">Keyword</td>
                <td><input type="text" id="keyword" name="keyword" class="textfield medium" />
                	<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="grid_reload(); return false;">Search</a>
                </td>
            </tr>
            </table>
        </form>
    </div>
</div>
<div id="datagrid" toolbar="#tb"></div>



<div id="dialog_container" style="padding:5px;width:600px;height:380px;">
	<form id="form_master">
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td class="label" style="width:150px">Kode Invoice</td>
        <td><input id="kode_invoice" /></td>
    </tr>
    <tr>
        <td class="label">Kode Kontrak</td>
        <td><input id="kode_kontrak" /></td>
    </tr>
    <!--  <tr>
        <td class="label">Kode Vendor</td>
        <td><input id="kode_kontrak" /></td>
    </tr> -->
     <tr>
        <td class="label">Nama_Vendor</td>
        <td><input id="nama_vendor" /></td>
    </tr>
     <tr>
        <td class="label">Harga</td>
        <td><input id="harga" /></td>
    </tr>
    <tr>
        <td class="label">Tgl Invoice</td>
        <td><input id="tgl_invoice" /></td>
    </tr>
    <tr>
        <td class="label">Status</td>
        <td><input id="status" /></td>
    </tr>     
    <tr>
        <td class="label">Total Bayar</td>
        <td><input id="total_bayar" /></td>
    </tr>                  
    <tr>
        <td class="label">Keterangan</td>
        <td><input id="keterangan" /></td>
    </tr>       
    </table>
	</form>
</div>